#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time: 21-3-5 下午8:08
# @Author: HuangChuQin
# @Site: 
# @File: SHA256.py
# @Software: PyCharm

import sys

str_source = None
str_result = None


def usage():
    print('********', sys.argv[0], '********')
    print('|--返回用SHA256加密后的字符串')
    print('|--结果会直接打印并输出到当前目录下的 sha256.ini 文件')
    print('|--使用说明')
    print('   格式： python SHA256.py 源字符串')
    print('   比如： python SHA256.py abc')
    print('********', sys.argv[0], '********')


def get_opt():
    global str_source
    if len(sys.argv) <= 1:
        print('入参错误')
        usage()
        sys.exit()
    str_source = sys.argv[1:][0]
    print("Input:",str_source)
    if str_source is None:
        print('入参错误')
        usage()
        sys.exit()


class SHA256:
    def __init__(self):
        # 64个常量
        # 图中Kt
        self.constants = (
            0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
            0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
            0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
            0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
            0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
            0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
            0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
            0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
            0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
            0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
            0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
            0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
            0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
            0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
            0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
            0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2)
        # 迭代初始值，h0,h1,...,h7
        self.h = (
            0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
            0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19)

    # x循环右移b个bit
    # right rotate b bit
    def right_rotate(self, x, b):
        return ((x >> b) | (x << (32 - b))) & ((2 ** 32) - 1)

    # 信息预处理。附加填充和附加长度值
    def pad(self, w):
        return bytes(w, "ascii") + b"\x80" + (b"\x00" * ((55 if (len(w) % 64) < 56 else 119) - (len(w) % 64))) + ((len(w) << 3).to_bytes(8, "big"))

    def compress(self, wt, kt, a, b, c, d, e, f, g, h):
        return ((h + (self.right_rotate(e, 6) ^ self.right_rotate(e, 11) ^ self.right_rotate(e, 25)) + (
            (e & f) ^ (~e & g)) + wt + kt) + (
                    self.right_rotate(a, 2) ^ self.right_rotate(a, 13) ^ self.right_rotate(a, 22)) + (
                    (a & b) ^ (a & c) ^ (b & c))) & ((2 ** 32) - 1), a, b, c, (d + (
            h + (self.right_rotate(e, 6) ^ self.right_rotate(e, 11) ^ self.right_rotate(e, 25)) + (
                (e & f) ^ (~e & g)) + wt + kt)) & ((2 ** 32) - 1), e, f, g

    def hash(self, message):
        message = self.pad(message)
        digest = list(self.h)

        for i in range(0, len(message), 64):
            S = message[i: i + 64]
            W = [int.from_bytes(S[e: e + 4], "big") for e in range(0, 64, 4)] + ([0] * 48)

            # 构造64个word
            for j in range(16, 64):
                W[j] = (W[j - 16] + (
                    self.right_rotate(W[j - 15], 7) ^ self.right_rotate(W[j - 15], 18) ^ (W[j - 15] >> 3)) + W[
                            j - 7] + (self.right_rotate(W[j - 2], 17) ^ self.right_rotate(W[j - 2], 19) ^ (
                    W[j - 2] >> 10))) & ((2 ** 32) - 1)

            a, b, c, d, e, f, g, h = digest

            for j in range(64):
                a, b, c, d, e, f, g, h = self.compress(W[j], self.constants[j], a, b, c, d, e, f, g, h)

        return "".join(format(h, "02x") for h in b"".join(
            d.to_bytes(4, "big") for d in
            [(x + y) & ((2 ** 32) - 1) for x, y in zip(digest, (a, b, c, d, e, f, g, h))]))


def main():
    global str_result
    global str_source
    encoder = SHA256()
    str_result = encoder.hash(str_source)

    print("Output:",str_result)

    with open("SHA256.ini", "w") as file:
        file.write(str_result)

    return str_result

if __name__ == "__main__":
    get_opt()
    main()
